package com.youkeda.DesertCamel.dao.mapper;

import com.youkeda.DesertCamel.dao.dataobject.MrBookRecord;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import java.util.List;


@Mapper
public interface MrBookRecordMapper {

    @Delete({
            "delete from mr_book_record",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);


    @Insert({
            "insert into mr_book_record (id, user_id, ",
            "company_id, meeting_room_id, ",
            "due_time, valid, book_time, ",
            "outnumber, gmt_created, ",
            "gmt_modified)",
            "values (#{id,jdbcType=VARCHAR}, #{userId,jdbcType=VARCHAR}, ",
            "#{companyId,jdbcType=VARCHAR}, #{meetingRoomId,jdbcType=VARCHAR}, ",
            "#{dueTime,jdbcType=TIMESTAMP}, #{valid,jdbcType=BIT}, #{bookTime,jdbcType=TIMESTAMP}, ",
            "#{outnumber,jdbcType=INTEGER}, #{gmtCreated,jdbcType=TIMESTAMP}, ",
            "#{gmtModified,jdbcType=TIMESTAMP})"
    })
    int insert(MrBookRecord record);


    @Select({
            "select",
            "id, user_id, company_id, meeting_room_id, due_time, valid, book_time, outnumber, ",
            "gmt_created, gmt_modified",
            "from mr_book_record",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_id", property = "companyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "meeting_room_id", property = "meetingRoomId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "due_time", property = "dueTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "valid", property = "valid", jdbcType = JdbcType.BIT),
            @Result(column = "book_time", property = "bookTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "outnumber", property = "outnumber", jdbcType = JdbcType.INTEGER),
            @Result(column = "gmt_created", property = "gmtCreated", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "gmt_modified", property = "gmtModified", jdbcType = JdbcType.TIMESTAMP)
    })
    MrBookRecord selectByPrimaryKey(String id);


    @Update({
            "update mr_book_record",
            "set user_id = #{userId,jdbcType=VARCHAR},",
            "company_id = #{companyId,jdbcType=VARCHAR},",
            "meeting_room_id = #{meetingRoomId,jdbcType=VARCHAR},",
            "due_time = #{dueTime,jdbcType=TIMESTAMP},",
            "valid = #{valid,jdbcType=BIT},",
            "book_time = #{bookTime,jdbcType=TIMESTAMP},",
            "outnumber = #{outnumber,jdbcType=INTEGER},",
            "gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},",
            "gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(MrBookRecord record);


    /**
     * 通过用户id查询该用户所有的预订信息
     *
     * @param userId 用户id
     * @return 用户的预定所有信息
     */
    @Select({
            "select",
            "id, user_id, company_id, meeting_room_id, due_time, valid, book_time, outnumber, ",
            "gmt_created, gmt_modified",
            "from mr_book_record",
            "where user_id = #{userId,jdbcType=VARCHAR}",
            "order by gmt_created desc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_id", property = "companyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "meeting_room_id", property = "meetingRoomId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "due_time", property = "dueTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "valid", property = "valid", jdbcType = JdbcType.BIT),
            @Result(column = "book_time", property = "bookTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "outnumber", property = "outnumber", jdbcType = JdbcType.INTEGER),
            @Result(column = "gmt_created", property = "gmtCreated", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "gmt_modified", property = "gmtModified", jdbcType = JdbcType.TIMESTAMP)
    })
    List<MrBookRecord> selectAllByUserID(String userId);


    /**
     * 通过记录id和有效值修改有效值
     *
     * @param id    记录id
     * @param valid 是否有效
     * @return 是否成功
     */
    @Update({
            "update mr_book_record",
            "set valid = #{valid,jdbcType=BIT}",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateValidByPrimaryKey(String id, boolean valid);
}